package com.chuan.it_market.controller.user_controller;

import com.chuan.it_market.controller.BaseController;
import com.chuan.it_market.dao.SellerDao;
import com.chuan.it_market.pojo.dto.ResponseDTO;
import com.chuan.it_market.pojo.entity.Buyer;
import com.chuan.it_market.pojo.entity.Seller;
import com.chuan.it_market.service.BuyerService;
import com.chuan.it_market.service.SellerService;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    BuyerService buyerService;
    @Resource
    SellerService sellerService;



    @RequestMapping("/buyer/buyerDetail")
    String buyerDetail(Model model){
        Integer bno=getUsreId();
        Buyer buyer=buyerService.selectBuyerByPrimaryKey(bno);
        model.addAttribute("user",buyer);
        return "buyer/center/buyer-detail";
    }

    //修改买家
    @RequestMapping("/buyer/update")
    @ResponseBody
    ResponseDTO buyerUpdate(Buyer buyer, MultipartFile pic){
        Integer bno=getUsreId();
        buyer.setBno(bno);
        if(!pic.isEmpty()){
            String imgUrl=uploadFile("/upload/goods/",pic);
            buyer.setIcon(imgUrl);
        }
        Buyer buyer1 =buyerService.selectBuyerByphone(buyer.getPhone());

        //没有修改手机号
            if(buyer1!=null&&buyer1.getBno().equals(bno)){
                int i= buyerService.update(buyer);
                if(i!=0){
                    return ResponseDTO.ok("修改成功");
                }
                else {
                    return ResponseDTO.fail("修改失败1");
                }
            }
            //修改了一个数据库没有的手机号
            else if(buyer1==null){
                int i= buyerService.update(buyer);
                if(i!=0){
                    return ResponseDTO.ok("修改成功");
                }
                else {
                    return ResponseDTO.fail("修改失败2");
                }
            }
            //修改了一个数据库存在的手机号 并且那个手机号不是自己之前的
            else{
                return ResponseDTO.fail("手机号重复");
            }

        }

    @RequestMapping("/seller/sellerDetail")
    String sellerDetail(Model model){
        Integer sno=getUsreId();
        Seller seller=sellerService.selectSellerByPrimaryKey(sno);
        model.addAttribute("user",seller);
        return "seller/center/seller-detail";
    }
    @RequestMapping("/seller/update")
    @ResponseBody
    ResponseDTO sellerUpdate(Seller seller,MultipartFile pic){
        Integer sno=getUsreId();
        seller.setSno(sno);
        if(!pic.isEmpty()){
            String imgUrl=uploadFile("/upload/goods/",pic);
            seller.setIcon(imgUrl);
        }
        Seller seller1 =sellerService.selectSellerByphone(seller.getPhone());

        //没有修改手机号
        if(seller1!=null&&seller1.getSno().equals(sno)){
            int i= sellerService.update(seller);
            if(i!=0){
                return ResponseDTO.ok("修改成功");
            }
            else {
                return ResponseDTO.fail("修改失败1");
            }
        }
        //修改了一个数据库没有的手机号
        else if(seller1==null){
            int i= sellerService.update(seller);
            if(i!=0){
                return ResponseDTO.ok("修改成功");
            }
            else {
                return ResponseDTO.fail("修改失败2");
            }
        }
        //修改了一个数据库存在的手机号 并且那个手机号不是自己之前的
        else{
            return ResponseDTO.fail("手机号重复");
        }

    }
}
